﻿using System;
using System.CodeDom.Compiler;
using System.Linq;

namespace FBGraph.ClientGenerator.CodeGen
{
    public abstract class Member : CodeGenObject
    {
        public Member()
        {
            this.CustomAttributes = new CustomAttributeList();
            this.XmlDocComments = new XmlDocCommentList();
        }

        public XmlDocCommentList XmlDocComments { get; private set; }
        public CustomAttributeList CustomAttributes { get; private set; }

        protected virtual void BeforeWrite() { }

        protected abstract void WriteDeclaration(CodeGenWriter writer);

        protected virtual void WriteOpenBody(CodeGenWriter writer)
        {
            writer.WriteLine('{');
        }

        protected virtual void WriteBody(CodeGenWriter writer) { }

        protected virtual void WriteCloseBody(CodeGenWriter writer)
        {
            writer.WriteLine('}');
        }

        public override void Write(CodeGenWriter writer)
        {
            this.BeforeWrite();

            writer.Write(this.XmlDocComments);
            writer.Write(this.CustomAttributes);
            this.WriteDeclaration(writer);
            this.WriteOpenBody(writer);
            using (writer.Indent())
            {
                this.WriteBody(writer);
            }
            this.WriteCloseBody(writer);
        }
    }
}